class Solution:
    def countSubTrees(self, n: int, edges: List[List[int]], labels: str) -> List[int]:
        linknodes=[[]for i in range(n)]
        visited=[0 for i in range(n)]
        results=[0 for i in range(n)]
        visited[0]=1
        for e in edges:
            if visited[e[1]]==1 and visited[e[0]]==0:
                linknodes[e[1]].append(e[0])
                visited[e[0]]=1
            else:
                linknodes[e[0]].append(e[1])
                visited[e[1]]=1
        def countsub(index):
            wordmap=[0 for j in range(26)]
            wordmap[ord(labels[index])-ord('a')]=1
            if len(linknodes[index])==0:
                results[index]=1
                return wordmap
            else:
                for i in linknodes[index]:
                    nwordmap=countsub(i)
                    for k in range(26):
                        wordmap[k]+=nwordmap[k]
                results[index]=wordmap[ord(labels[index])-ord('a')]
                return wordmap
        countsub(0)
        return results